package ensib.sti.ssu.classes;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.net.Uri.Builder;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.ImageView;

import ensib.sti.ssu.R;
import ensib.sti.ssu.beans.BlocNote;

public class Utilitaire {
	public final static int SORT_BY_PRIORITY=0x01;
	public final static int SORT_BY_DEADLINE=0x02;
	public final static int SORT_BY_TYPE=0x03;
	public final static int SORT_BY_TACHE_NAME=0x04;
	public final static int SORT_BY_DESCRIPTION=0x5;
	public final static int DIX_JOURS=0x337F9800;//ms
	public final static int VINGT_QUATRE_HEURE=23*3600000;//ms
	
	public static  List<BlocNote> trier(List<BlocNote> liste,int preference) {
		//true du plus grand au plus petit
		return trier(liste, preference, true);
	}
		
	public static  List<BlocNote> trier(List<BlocNote> liste,int preference,boolean order) {//true du plus grand au plus petit
		// TODO Auto-generated method stub
		 
		switch (preference) {
		 
			 
      case SORT_BY_PRIORITY:
    	  Collections.sort(liste, new Comparator<BlocNote>() {

				@Override
				public int compare(BlocNote n1, BlocNote n2) {
					// TODO Auto-generated method stub
					return n1.getPriorite()-n2.getPriorite() ;
				}
			});
    	  if(order) Collections.reverse(liste);//du plus grand au plus petit
			break;
      case SORT_BY_TACHE_NAME:
    	  Collections.sort(liste, new Comparator<BlocNote>() {

				@Override
				public int compare(BlocNote n1, BlocNote n2) {
					// TODO Auto-generated method stub
					return n1.getNomTache().compareTo(n2.getNomTache()) ;
				}
			});
			break;
      case SORT_BY_TYPE:
    	  Collections.sort(liste, new Comparator<BlocNote>() {

				@Override
				public int compare(BlocNote n1, BlocNote n2) {
					// TODO Auto-generated method stub
					return n1.getTypeTache()-n2.getTypeTache() ;
				}
			});
    	  if(order) Collections.reverse(liste);//du plus grand au plus petit
			break;
      case SORT_BY_DESCRIPTION:
    	  Collections.sort(liste, new Comparator<BlocNote>() {

				@Override
				public int compare(BlocNote n1, BlocNote n2) {
					// TODO Auto-generated method stub
					return n1.getDescription().compareTo(n2.getDescription()) ;
				}
			});
			break;
		default:
			Collections.sort(liste, new Comparator<BlocNote>() {

				@Override
				public int compare(BlocNote n1, BlocNote n2) {
					// TODO Auto-generated method stub
					//return  n1.getDeadLineDate()-n2.getDeadLineDate();
					Date date1=new Date(),
							date2=new Date(n1.getDeadLineDate()),
							date3=new Date(n2.getDeadLineDate());
					long d= parse(date1);
					long d1=parse(date2),
							d2=parse(date3) ;
					 
					if(d1>=d&&d2>=d){// d=0  4 5
						if(d1<d2)
							return (int) (d2-d1); 
					}
					 return (int) (d1-d2);
				   }

				private long parse(Date date1) {
					// TODO Auto-generated method stub
					return Long.parseLong((date1.getYear()+""+(date1.getMonth()<10?"0"+date1.getMonth():""+date1.getMonth())+""+(date1.getDate()<10?"0"+date1.getDate():""+date1.getDate() ).trim()));
					 
				}
			});
			if(order) Collections.reverse(liste);//du plus grand au plus petit
			break;
		}
		
		 return liste;
	}

	public static String prioriteTostring(int priorite,Context c) {
		String[] pri=c.getResources().getStringArray(R.array.priorite_arrays);
		 
		  return pri[(priorite-1)%(pri.length)];
		
	}
	public static String typeTacheTostring(int type,Context c) {
		String[] pri=c.getResources().getStringArray(R.array.type_tache_arrays);
		 
		  return pri[(type-1)%(pri.length)];
		
	}
	public static String preferenceTriTostring(int pref,Context c) {
		String[] pri=c.getResources().getStringArray(R.array.listchoixtri);
		 
		  return pri[(pref-1)%(pri.length)];
		
	}

	public static void showImage(String selectedImage, int imageViewId,Activity context ){
		 try {Builder bul = new android.net.Uri.Builder();
		bul.path(selectedImage);
		
			 showImage(bul.build(), (ImageView )context.findViewById(imageViewId),context);
	}catch (Exception e) {
		// TODO: handle exception
		Log.e("error", e.toString());
	}
		
	}
public static void showImage(Uri selectedImage, ImageView imageView,Activity context ){
	 
	 context.getContentResolver().notifyChange(selectedImage, null);
  
   ContentResolver cr = context.getContentResolver();
   Bitmap bitmap;
   try {
        bitmap = android.provider.MediaStore.Images.Media
        .getBitmap(cr, selectedImage);
 
       imageView.setImageBitmap(bitmap);
}catch (Exception e) {
	// TODO: handle exception
	Log.e("error", e.toString());
}
}
}
